import Vue from 'vue'
import Vuex from 'vuex'
import {getItem, getSessionItem, removeItem, setItem, setSessionItem} from "@/utils/auth";

import {decrypt, encrypt} from "@/utils/AES";
import {countryCode, getUserInfo, loginApi, reqApi} from "@/api/apiFox";
import {delCookie, getCookie, setCookie} from "@/utils/Cookie";
import router from "@/router";

Vue.use(Vuex)
const userKey = 'user'
const tokenKey = 'admin'

export default new Vuex.Store({
  state: {
    token: getCookie('like-admin'),
    countryCode:getItem('country'),
    userInfo:getSessionItem('userInfo')
  },
  getters: {},
  mutations: {
    // 更新token
    saveToken(state, data) {
      state.token = data.data
      setCookie('like-admin',data.data)
    },
    // 注册的token
    saveRegToken(state, data) {
      console.log(data)
      setItem('phone', {
        account: data.account,
        internationalCode:data.countryCode,
        pwd:data.password,
        isReg:true
      })
    },

    setUserInfo(state,data){
      state.UserInfo = data
      setSessionItem('userInfo',data)
    },

    setCountryCode(state, data){
      state.countryCode = data
      setItem('country',data)
    },

    // 推出登录
    removeLogin(state, data){
      state.token = ''
      delCookie('like-admin')
      removeItem('tip')
      location.reload()
    }

  },
  actions: {
    loginSetToken({commit}, params) {
      return new Promise((resolve, reject) => {
        let form = {
          account:params.account,
          internationalCode:params.internationalCode,
          pwd: encrypt(params.pwd)}
        loginApi(form).then(res => {
          commit('saveToken', res)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },

    // 注册信息
    regSetUser({commit}, params){
      return new Promise((resolve,reject)=>{
        let form = {
          account: params.account, 	//账号 1
          countryCode: params.countryCode, 	//国家码 1
          invitationCode: params.invitationCode, 	//邀请码1
          password: encrypt(params.password), 	//密码1
          uuid: params.uuid, //随机码1
          validateCode: params.validateCode //图形验证码1
        }
        reqApi(form).then(res=>{
          commit('saveRegToken',params)
          resolve(res)
        }).catch(err=>{
          reject(err)
        })
      })
    },

    // 获取用户信息
    getUSerInfo({commit},params){
        return new Promise((resolve,reject)=>{
          getUserInfo().then(res=>{
            resolve(res)
            commit('setUserInfo',res)
          }).catch(err=>{
            reject(err)
          })
        })
    },

    getCountryCode({commit},params){
      return new Promise((resolve,reject)=>{
        countryCode().then(res=>{
            commit('setCountryCode', res.data)
            resolve(res)
        }).catch(err=>{
          reject(err)
        })
      })
    },

    // 推出登录
    removeLogins({commit},params){
      commit('removeLogin')
      router.push('/login').catch(err=>{})
    }

  },
  modules: {}
})
